package com.zhugang.week13.hash;

import java.util.HashMap;
import java.util.Map;

/**
 * @program algorithms
 * @description: WordsFrequency
 * @author: chanzhugang
 * @create: 2022/12/04 22:08
 */
public class WordsFrequency {

    /**
     * 16.02 单词频率
     * https://leetcode.cn/problems/words-frequency-lcci/
     *
     * @param book
     */
    Map<String, Integer> cacheMap = new HashMap<>();

    public WordsFrequency(String[] book) {
        /**
         * 哈希表缓存
         */
        for (String str : book) {
            int count = 1;
            if (cacheMap.containsKey(str)) {
                count += cacheMap.get(str);
            }
            cacheMap.put(str, count);
        }
    }

    public int get(String word) {
        if (cacheMap.containsKey(word)) {
            return cacheMap.get(word);
        }
        return 0;
    }
}